package com.ibm.android;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.ProgressBar;

import com.ibm.android.camera.CameraSource;
import com.ibm.android.camera.HttpCamera2;

public class OnlinePayment extends Activity implements Callback {
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private CameraSource mCameraSource;
	private boolean mPreviewRunning;
	private  LayoutInflater mInflater;
		
	private static final String TAG = "Online payment";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mInflater = LayoutInflater.from(this);
		final Button scanButton = (Button) findViewById(R.id.scan);
        scanButton.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				setContentView(R.layout.product_info);
				return true;
			}
		});
        
    	final Button profilButton = (Button) findViewById(R.id.profil);
        profilButton.setOnTouchListener(new View.OnTouchListener() {
			
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				setContentView(R.layout.user_profil);
				return true;
			}
		});
        
//        final Button saveButton = (Button) findViewById(R.id.save_button);
//        profilButton.setOnTouchListener(new View.OnTouchListener() {
//			
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub				
//				setContentView(R.layout.main);			
//				return true;
//		   }
//		});
//        
//        final Button cancelButton = (Button) findViewById(R.id.cancel_button);
//        cancelButton.setOnTouchListener(new View.OnTouchListener() {
//			
//			public boolean onTouch(View v, MotionEvent event) {
//				// TODO Auto-generated method stub
//				setContentView(R.layout.main);
//				return true;
//			}
//		});
//		
		mPreviewRunning = false;
		mCameraSource = new HttpCamera2("http://192.168.184.1:8080/");
		
		mSurfaceView = (SurfaceView) findViewById(R.id.SVCamera);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
		// XXX stopPreview() will crash if preview is not running
		if (mPreviewRunning) {
			mCameraSource.stopPreview();
		}

		Camera.Parameters p = mCameraSource.getParameters();
		p.setPreviewSize(w, h);
		mCameraSource.setParameters(p);
		try {
			mCameraSource.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mCameraSource.startPreview();
		mPreviewRunning = true;
	}


	public void surfaceCreated(SurfaceHolder holder) {
		mCameraSource.open();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		mCameraSource.release();
		mPreviewRunning = false;
	}
}